/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package io.midi;

import java.util.List;
import javax.sound.midi.MidiMessage;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.Variable;

/**
 *
 * @author eisental
 */
public abstract class MidiSystemController {
    protected MidiInListener listener;
    protected boolean isInput;
    protected int portId = -1;

    public abstract void open() throws IllegalArgumentException;
    public abstract void close() throws IllegalArgumentException;
    public abstract void sendMidiMessage(MidiMessage msg, long timestamp) throws IllegalArgumentException;
    public abstract boolean isOpen();

    public void setPort(int index, boolean isInput) throws IllegalArgumentException {
        this.portId = index;
        this.isInput = isInput;
        if (isOpen()) {
            close();
            open();
        }
    }

    public void sendMidi(Variable in, long timestamp) throws IllegalArgumentException {
        if (in.getDataType().matches(DataType.MIDI)) {
            MidiMessage out = null;
            out = (MidiMessage)in.getValue(null);
            sendMidiMessage(out, timestamp);
        } else if (in.getDataType().matches(DataType.MIDI, DataType.MATCH_ARRAY)) {
            List<Object> v = ((List<Object>)in.getValue(null));
            for (Object element : v) {
                MidiMessage out = (MidiMessage)element;
                sendMidiMessage(out, timestamp);
            }
        } else if (in.getDataType().matches(DataType.VAR, DataType.MATCH_ARRAY)) {
            int i=0;
            long curtimestamp = -1;
            List<Object> v = ((List<Object>)in.getValue(null));
            while (i<v.size()) {
                Object element = v.get(i);
                DataType elementType = DataType.findType(element);
                if (elementType.matches(DataType.LONG) || elementType.matches(DataType.INT)) {
                    curtimestamp = (Long)element;
                    if (i+1<v.size()) {
                        i++;
                        Object nextElement = v.get(i);
                        DataType nextType = DataType.findType(nextElement);
                        if (nextType.matches(DataType.MIDI)) {
                            sendMidiMessage((MidiMessage)nextElement, curtimestamp);
                            curtimestamp = -1;
                        } else throw new IllegalArgumentException("Expecting MIDI variable after long or int variable to send a var array");
                    } else if (elementType.matches(DataType.MIDI)) {
                        sendMidiMessage((MidiMessage)element, -1);
                    }
                    i++;
                }
            }
        } else {
            throw new IllegalArgumentException("Can't send variable of type " + in.getDataType().getName() + " through MIDI port.");
        }
    }

    public void setMidiInListener(MidiInListener listener) {
        this.listener = listener;
    }

    public abstract String getPortName();

    public abstract int findPortByName(String string);

    public int getPortId() {
        return portId;
    }
}
